home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / duftp / dip.c < prev    next >
C/C++ Source or Header  |  1995-06-17  |  4KB  |  163 lines

  1. /*
  2.     DUFTP
  3. */
  4.  
  5. // LINKS to DIP for dialup access
  6.  
  7. // This module is a little obscure - sorry, but it has to be that way to
  8. // allow the DIP program to be monitored without recompiling it and
  9. // still get some GEM feedback on whats happening.
  10. //
  11. // Major obscure bits are the fork() (ok, Unix heads have no problem here)
  12. // and the use of Set_timer_callback() - this is a DULIB function that
  13. // effectively multithreads a GEM program by calling a callback at regular
  14. // intervals (automatically), so I can poll for the DIP program dieing
  15. // and open and close dialogs as a result - needed 'coz you cann't call
  16. // GEM from inside a signal handler.
  17.  
  18. #include <DULIB.H>
  19. #include <UNISTD.H>
  20. #include <SIGNAL.H>
  21. #include <FCNTL.H>
  22. #include <sys/ioctl.h>
  23. #include <sys/socket.h>
  24. #include <net/if.h>
  25. #include <sockios.h>
  26. #include "duftp_n.h"
  27. #include "globals.h"
  28.  
  29. int dip_o_file;
  30. short dip_online=0;
  31. short dip_running=0;
  32. __Sigansi original_SIGCHLD;
  33.  
  34. // Flag that we've noticed that DIP has died, so that the GEM based timer
  35. // callback will have a bit of a snoop to see if SLIP is up yet.
  36. void dip_died(int a)
  37. {
  38.     signal(SIGCHLD,SIG_IGN);
  39.     dip_running=2;    
  40. }
  41.  
  42. short dip_close_connected_dialog(void)
  43. {
  44.     close_dialog(Dip_connected);        //Close the connected dialog
  45.     Set_timer_callback(0,NULL);            //Clear the timer callback, we don't need it anymore
  46.     return TRUE;
  47. }
  48.  
  49. // detect whether the slip link actually came up or not
  50. short dip_detect_connect(void)
  51. {
  52.     int sock;
  53.     struct ifreq ifr;
  54.     
  55.     sock = socket (PF_INET, SOCK_DGRAM, 0);
  56.     if (sock>0)
  57.     {
  58.         strcpy (ifr.ifr_name, "sl0");
  59.         if (ioctl (sock, SIOCGIFFLAGS, &ifr) < 0) {
  60.             form_alert(1,"[3][ SLIP: sl0 | Cannot get FLAGS. ][ ok ]");
  61.         }else{
  62.             if (ifr.ifr_flags&IFF_UP)            // Did DIP do the job?
  63.             {
  64.                 activate_dialog(Dip_connected,"Connected", DIAL_NO_CLOSE);    // Open a dialog to say we're connected
  65.                 Set_timer_callback(4000,&dip_close_connected_dialog);        // Set a timed callback to close the connected dialog for us
  66.                 dip_online=1;                                                // Flag slip link as being up
  67.             }else{
  68.                 Set_timer_callback(0,NULL);            //Clear the timer callback, we don't need it anymore
  69.                 dip_running=0;
  70.                 dip_online=0;
  71.             }
  72.         }
  73.         close(sock);
  74.     }
  75.     
  76.     return TRUE;
  77. }
  78.  
  79. // Snoop the dip_running signal to see if DIP is actually still going, or if
  80. // it is dead. A DULIB pseudo multitasked routine, we can actually use GEM
  81. // commands here, so we can close the DIP dialog.
  82. short dip_detect_dip_die(void)
  83. {
  84.     if (dip_running<2) return FALSE;    // Is DIP still alive? 0=not running, 1=running, 2=dieing/dead
  85.                                         // If dieing/dead, then start the check for slip link (sl0) sequence
  86.     close_dialog(Dip_dialing);            //Close the dialing dialog
  87.     Set_timer_callback(4000,&dip_detect_connect);    // Set a timed callback to check if DIP managed to enable sl0
  88.                                                     // (give it 4 seconds for the link to be up & stable)
  89.     return TRUE;
  90. }
  91.  
  92. short dip_dialup(void)
  93. {
  94.     char d[FMSIZE];
  95.     
  96.     if (dip_online) return TRUE;
  97.     
  98.     if (form_alert(1,"[2][ DIAL-UP INTERNET PROTOCOL | Open a SLIP connection ? ][ Yes | No ]")==2) return TRUE;
  99.  
  100. // Change to the extra's directory where I've put DIP
  101.     getcwd(d,FMSIZE);
  102.     chdir(initial_dir);
  103.     chdir("extras");
  104.  
  105.     signal(SIGCHLD,dip_died);
  106.     dip_online=1;
  107.     dip_running=1;
  108.  
  109.     Set_timer_callback(1000,&dip_detect_dip_die);
  110.  
  111. // Spawn DIP
  112.     activate_dialog(Dip_dialing,"DIP DIALUP",DIAL_NO_CLOSE);
  113.  
  114.     dip_o_file=creat("dip.dbg",O_WRONLY);
  115.  
  116.     if (fork()==0)
  117.     {
  118.         dup2(dip_o_file, 1);
  119.         dup2(dip_o_file, 2);
  120.         execl("dip.ttp","dip.ttp","slip.dip",NULL);
  121.         exit(0);
  122.     }
  123.  
  124. // Restore the current directory
  125.     chdir(d);
  126.  
  127.     return TRUE;
  128. }
  129.  
  130. short dip_hangup(void)
  131. {
  132.     char d[FMSIZE];
  133.     
  134.     if (!dip_online) return TRUE;
  135.     
  136.     if (form_alert(1,"[2][ DIAL-UP INTERNET PROTOCOL | Close SLIP connection ? ][ Yes | No ]")==2) return TRUE;
  137.  
  138. // Change to the extra's directory where I've put DIP
  139.     getcwd(d,FMSIZE);
  140.     chdir(initial_dir);
  141.     chdir("extras");
  142.  
  143. // Spawn DIP
  144.     signal(SIGCHLD,original_SIGCHLD);
  145.     if (fork()==0)
  146.     {
  147.         dup2(dip_o_file, 1);
  148.         dup2(dip_o_file, 2);
  149.         execl("dip.ttp","dip,ttp","-k",NULL);
  150.     }
  151.     wait(0);
  152.     close(dip_o_file);
  153.     
  154.     signal(SIGCHLD,SIG_IGN);
  155.  
  156. // Restore the current directory
  157.     chdir(d);
  158.  
  159.     dip_online=0;
  160.  
  161.     return TRUE;
  162. }
  163.